#include "unitls.h"


QString get_file_name_use_path(QString full_file_path)
{
    int file_name_start_index = full_file_path.lastIndexOf("/");
    QString node_name = full_file_path.mid(file_name_start_index+1);
    return node_name;
}
/**
 * @brief 替换文件名,返回组合新旧文件组合字符串,用于各窗口传递文件
 * @function: replace_file_name
 * @full_file_path_old[in] 新文件全路经 QString /etc/a.txt
 * @new_file_name[in] 文件全路经 QString b.txt
 * @return QString(旧文件全路径^^^新文件全路经)
 *
 */
QString replace_file_name(QString full_file_path_old, QString new_file_name)
{
    QFileInfo fi(full_file_path_old);
    QString new_full_file_path = full_file_path_old;
    new_full_file_path = new_full_file_path.left(new_full_file_path.length() - get_file_name_use_path(full_file_path_old).length());
    new_full_file_path.append(new_file_name);

    if (!fi.exists()){
        return new_full_file_path;
    }
    if (fi.isDir()){
        QDir dir(full_file_path_old);
        dir.rename(full_file_path_old, new_full_file_path);
    }
    else{
        QFile file(full_file_path_old);
        file.rename(full_file_path_old, new_full_file_path);
    }
    QString result(full_file_path_old);
    result.append("^^^");
    result.append(new_full_file_path);
    return result;
}

/**
 * @brief 接收新旧文件组合字符串,取出旧文件全路径
 * @function: get_old_path_from_replace_str
 * @replace_str[in] 新旧文件组合字符串 QString(旧文件全路径^^^新文件全路经)
 * @return QString(旧文件全路径)
 *
 */
QString get_old_path_from_replace_str(QString replace_str)
{
    return replace_str.split("^^^")[0];
}

/**
 * @brief 接收新旧文件组合字符串,取出新文件全路径
 * @function: get_old_path_from_replace_str
 * @replace_str[in] 新旧文件组合字符串 QString(旧文件全路径^^^新文件全路经)
 * @return QString(新文件全路径)
 *
 */
QString get_new_file_name_from_replace_str(QString replace_str)
{
    return replace_str.split("^^^")[1];
}
/**
 * @brief 输入文件全路径，获取文件图标
 * @function: get_file_icon
 * @file_full_path[in] 文件全路径
 * @return QString(新文件全路径)
 *
 */
QIcon get_file_icon(QString file_full_path)
{
    QFileInfo fileInfo(file_full_path);
    if(fileInfo.isDir()){
        return QIcon(QString(":/file_system/dir.png"));
    }
//    else if(file_full_path.endsWith(".doc") || file_full_path.endsWith(".wps")
//            || file_full_path.endsWith(".docx")){
//        return QIcon(QString(":/file_system/doc.png"));
//    }
    else if(file_full_path.endsWith(".html") || file_full_path.endsWith(".htmls")){
        return QIcon(QString(":/file_system/html.png"));
    }
    else if(file_full_path.endsWith(".py")){
        return QIcon(QString(":/file_system/python.png"));
    }
    else if(file_full_path.endsWith(".txt")){
        return QIcon(QString(":/file_system/txt.png"));
    }
    else if(file_full_path.endsWith(".markdown") || file_full_path.endsWith(".md")){
        return QIcon(QString(":/file_system/markdown.png"));
    }
    else{
        return QIcon(QString(":/file_system/unknow.png"));
    }
}
